/**
 * Created by Administrator on 2017/9/20.
 */
angular.module('module.recommended')
    .controller('RecommendedController', RecommendedController);
RecommendedController.$inject = ['RecommendedService', 'FileUpload', '$localStorage', '$state'];
function RecommendedController(RecommendedService, FileUpload, $localStorage, $state) {
    var vm = this;
    if (!$localStorage[environmental + "shopid"]) {
        $localStorage.$reset();
        $state.go('login');
    }
    vm.shopid = $localStorage[environmental + "shopid"];
    vm.showsubmit = false;
    vm.foodinfo = [];
    //vm.submitbtn = submitbtn;
    vm.edit = edit;
    vm.isEdit = false; // 动态框

    // 往下移
    vm.down = down;

    // 往上移
    vm.up = up;

    // 保存删除
    vm.deleteImages = deleteImages;
    vm.sureBtn = sureBtn; // 模态框
    vm.cancelBtn = cancelBtn; // 模态框
    vm.uploadLogo = uploadLogo;
    vm.stateGo = stateGo;
    vm.remove = remove;

    function sureBtn() {
        vm.messageVisible = false;
    }

    function cancelBtn() {
        vm.messageVisible = false;
    }

    // 获取推荐菜品信息
    RecommendedService.getFoodInfo(vm.shopid).$promise.then(function (data) {
        console.log(data);
        vm.foodinfo = data;
        if (vm.foodinfo.length >= 5) {
            vm.showrecommended = false;
        }
        else {
            vm.showrecommended = true;
        }
    }, function (error) {
        console.log(error);
    });

    // 上传图片及保存菜品信息
    function uploadLogo(file) {
        if (file) {
            if (file.size > 4 * 1024 * 1024) {
                vm.messageVisible = true;
                vm.message = "请上传小于4M的图片";
                return;
            }
            FileUpload.setFile(file);
            $state.go("recommended-edit");
        }
    }

    // 点击编辑操作
    function edit() {
        vm.isEdit = !vm.isEdit;
        if (vm.foodinfo.length >= 0 && vm.foodinfo.length < 5) {
            vm.showrecommended = true;
        }
    }

    // 排序操作
    // 上移
    function up($index) {
        var demo;
        if ($index == 0) {
            return;
        }
        vm.foodinfo[$index].positionid--;
        vm.foodinfo[$index - 1].positionid++;
        demo = vm.foodinfo[$index];
        vm.foodinfo[$index] = vm.foodinfo[$index - 1];
        vm.foodinfo[$index - 1] = demo;
    }

    // 下移
    function down($index) {
        var demo;
        if ($index == vm.foodinfo.length - 1) {
            return;
        }
        vm.foodinfo[$index].positionid++;
        vm.foodinfo[$index + 1].positionid--;
        demo = vm.foodinfo[$index];
        vm.foodinfo[$index] = vm.foodinfo[$index + 1];
        vm.foodinfo[$index + 1] = demo;
    }


    function remove($index) {
        // 保存推荐菜品信息
        var marktips = vm.foodinfo[$index];
        vm.foodinfo.splice($index, 1);
        // 改变删除后的数组的positionID
        for (var j = $index; j < vm.foodinfo.length; j++) {
            vm.foodinfo[j].positionid--;
        }
    }

    function deleteImages() {
        if (vm.foodinfo.length > 0) {
            RecommendedService.addFoodInfo(vm.foodinfo, function (data) {
                edit();
            }, function (error) {
                vm.messageVisible = true;
                vm.message = "修改失败!";
                edit();
            });
        } else {
            RecommendedService.deleteFoodInfo(vm.shopid, function (data) {
                edit();
            }, function (error) {
                vm.messageVisible = true;
                vm.message = "修改失败!";
                edit();
            });
        }

    }

    // 跳转至二维码修改
    function stateGo(foodid) {
        if (vm.isEdit) {
            return;
        }
        $state.go("recommendedcode", {shopid: vm.shopid, foodid: foodid});
    }
}